package com.woniuxy.dudugua.service.impl;

import com.woniuxy.dudugua.entity.*;
import com.woniuxy.dudugua.mapper.*;
import com.woniuxy.dudugua.service.UserCouponsService;
import com.woniuxy.dudugua.util.view.CouponsStoreView;
import com.woniuxy.dudugua.util.view.CouponsView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserCouponsServiceImpl implements UserCouponsService {
    @Autowired
    private UserCouponsMapper userCouponsMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public int updateStatusById(int id, int status) {
        UserCoupons userCoupons = userCouponsMapper.selectByPrimaryKey(id);
        userCoupons.setStatus(status);
        int i = userCouponsMapper.updateByPrimaryKey(userCoupons);
        return i;
    }

    @Override
    public List<CouponsStoreView> selectAllCoupons(int id) {
        List<CouponsStoreView> list = null;
        if(id == 0){
            list = couponMapper.selectAllCouponsView();
        }else {
            list = couponMapper.selectUserCoponsView(id);
        }
        if(list.size() == 0){
            return null;
        }
        for(int i = 0 ; i < list.size();i++){
            CouponsStoreView view = list.get(i);
            if(view.getUseScope() == 2){
                Store store = storeMapper.selectByPrimaryKey(view.getScopeId());
                view.setStoreName(store.getName());
            }
            if(view.getUseScope() == 3){
                Product product = productMapper.selectByPrimaryKey(view.getScopeId());
                view.setProductName(product.getProductName());
            }
            if(view.getUseType() != 0){
                Category category = categoryMapper.selectByPrimaryKey(view.getUseType());
                view.setUseTypeComment(category.getCategoryName());
            }
        }
        return list;
    }
    //返回2代表已经达到领取上限
    //优惠券已领取完
    @Override
    public synchronized int userGetCoupons(Integer userid, int couponid) {
        Coupon coupon = couponMapper.selectByPrimaryKey(couponid);
        UserCouponsExample e = new UserCouponsExample();
        UserCouponsExample.Criteria c = e.createCriteria();
        c.andCouponIdEqualTo(couponid);
        c.andUserIdEqualTo(userid);
        List<UserCoupons> userCoupons = userCouponsMapper.selectByExample(e);
        if(userCoupons != null && userCoupons.size() >= coupon.getUserGetNum()){
            return 2;
        }
        if(coupon.getSendSum() <= coupon.getSendNum()){
            return 3;
        }
        UserCoupons uc = new UserCoupons();
        uc.setStatus(1);
        uc.setAddTime(new Date());
        uc.setCouponId(couponid);
        uc.setUserId(userid);
        int insert = userCouponsMapper.insert(uc);
        return 1;
    }
}
